package com.slf.flow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.slf.common.utils.Result;
import com.slf.flow.core.model.Variable;
import com.slf.flow.core.service.VariableService;
import com.slf.flow.model.JsonBean;
import oracle.jdbc.proxy.annotation.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 18812
 */
@RestController
@RequestMapping("/variable")
public class VariableController {

    @Autowired
    private VariableService service;

    @GetMapping("/list")
    public Result<IPage<Variable>> list(@RequestParam(name = "page",defaultValue = "1")Integer page, @RequestParam(name = "limit",defaultValue = "1")Integer size){
        Result<IPage<Variable>> result = new Result<IPage<Variable>>();
        try {
            Page<Variable> createDate = service.page(new Page<Variable>(page, size), new QueryWrapper<Variable>().orderByDesc("create_date"));
            result.ok(createDate);
        }catch (Exception e){
            result.error();
        }

        return result;
    }

    @GetMapping("get/{id}")
    public Result<Variable> get(@PathVariable("id") String id) {
        Result<Variable> result = new Result<>();
        try {
            Variable variable = service.getById(id);
            result.ok(variable);
        }catch (Exception e){
            result.error();
        }

        return result;
    }

    @DeleteMapping("delete/{id}")
    public Result<Boolean> delete(@PathVariable("id") String id){
        Result<Boolean> result = new Result<>();
        try {
            boolean b = service.removeById(id);
            result.ok(b);
        }catch (Exception e){
            result.error();
        }

        return result;
    }

    @PutMapping("save")
    public Result<Boolean> update(@RequestBody Variable t){
        Result<Boolean> result = new Result<>();
        try {
            boolean b = service.saveOrUpdate(t);
            result.ok(b);
        }catch (Exception e){
            result.error();
        }

        return result;
    }

    @PostMapping("save")
    public Result<Boolean> save(@RequestBody Variable t){
        Result<Boolean> result = new Result<>();
        try {
            boolean b = service.saveOrUpdate(t);
            result.ok(b);
        }catch (Exception e){
            result.error();
        }

        return result;
    }


}
